package top.yihoxu.yojcodesandbox.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.StopWatch;
import top.yihoxu.yojcodesandbox.model.ExecuteMessage;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * @Author dth
 * @Date 2024/5/4 03:49
 * @PackageName:top.yihoxu.yojcodesandbox.utils
 * @ClassName: ProcessUtils
 * @Description: TODO
 * @Version 1.0
 */

/**
 * 进程工具类
 */
public class ProcessUtils {
    /**
     * 执行进程并获取信息
     *
     * @param runProcess
     * @param type
     * @return
     */
    public static ExecuteMessage runProcessAndGetMessage(Process runProcess, String type) {
        ExecuteMessage executeMessage = new ExecuteMessage();
        try {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            //等待程序执行获取错误码
            int exitValue = runProcess.waitFor();
            executeMessage.setExitVale(exitValue);
            //编译成功
            if (exitValue == 0) {
                System.out.println(type + "成功 ");
                //分批获取进程的输出
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
                //逐行读取
                // StringBuilder stringBuilder = new StringBuilder();
                ArrayList<String> outputList = new ArrayList<>();
                String compileOutputLine;
                while ((compileOutputLine = bufferedReader.readLine()) != null) {
                    // stringBuilder.append(compileOutputLine).append("\n");
                    outputList.add(compileOutputLine);

                }
                executeMessage.setMessage(StringUtils.join(outputList));
                executeMessage.setExitVale(exitValue);

            } else {
                //异常退出
                System.out.println(type + "失败");
                //分批获取进程的输出
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
                //逐行读取
                //StringBuilder stringBuilder = new StringBuilder();
                ArrayList<String> outputList = new ArrayList<>();
                String compileOutputLine;
                while ((compileOutputLine = bufferedReader.readLine()) != null) {
                    // stringBuilder.append(compileOutputLine).append("\n");
                    outputList.add(compileOutputLine);
                }
                // executeMessage.setMessage(stringBuilder.toString());
                executeMessage.setMessage(StringUtils.join(outputList));
                //分批获取进程的输出
                BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(runProcess.getErrorStream()));
                //逐行读取
                // StringBuilder errorStringBuilder = new StringBuilder();
                ArrayList<String> errorOutputStrList = new ArrayList<>();
                // 逐行读取
                String errorCompileOutputLine;
                while ((errorCompileOutputLine = errorBufferedReader.readLine()) != null) {
                    //  errorStringBuilder.append(errorCompileOutputLine).append("\n");
                    errorOutputStrList.add(errorCompileOutputLine);
                }
                //executeMessage.setErrorMessage(errorStringBuilder.toString());
                executeMessage.setErrorMessage(StringUtils.join(errorOutputStrList));
                executeMessage.setExitVale(exitValue);
            }
            stopWatch.stop();
            long lastTaskTimeMillis = stopWatch.getLastTaskTimeMillis();
            executeMessage.setTime(lastTaskTimeMillis);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return executeMessage;
    }
}
